<template>
  <div class="registerSelection">
    <el-row>
      <!--左侧图片 -->
      <el-col :span="10">
        <div class="login_img"></div>
      </el-col>
      <!-- 右侧 -->
      <el-col :span="14">
        <div class="login_img bg_img login_right">
          <!-- 中英文切换下拉菜单 -->

          <div class="language_dropdown">
            <switchLang></switchLang>
          </div>
          <div
            class="logo"
            :style="{
              'background-image': 'url(' + require('@/assets/img/login/' + $config.logoBgUrl) + ')',
              'margin-left': $config.logoBg_left,
            }"
          ></div>

          <div class="organization1">
            <div class="organization_title">
              <div class="organization_title_wordage">
                {{ $t('zhuce.zhuce1') }}
              </div>
              <!-- 登录方式 -->
              <div class="organization_title_way"></div>
            </div>

            <div class="organization_item">
              <el-form
                ref="form"
                label-position="left"
                label-width="180px"
                :model="form"
                :rules="rules"
                size="small"
              >
                <el-form-item
                  :label="registoType === '1' ? $t('zhuce.zhuce2') : $t('zhuce.zhuce33') + '：'"
                  prop="phoneOrEmail"
                >
                  <el-input
                    v-if="registoType === '1'"
                    v-model="form.phoneOrEmail"
                    style="width: 330px; height: 50px"
                    :placeholder="$t('zhuce.zhuce3')"
                    maxlength="100"
                  />
                  <el-input
                    v-if="registoType === '2'"
                    :placeholder="$t('zhuce.zhuce34')"
                    v-model="form.phoneOrEmail"
                    maxlength="100"
                  >
                    <el-select v-model="phoneCode" slot="prepend" style="width: 90px">
                      <el-option
                        :label="item.paramValue"
                        :value="item.id"
                        v-for="(item, index) in phoneCodeList"
                        :key="index"
                      ></el-option>
                    </el-select>
                  </el-input>
                </el-form-item>
                <el-form-item :label="$t('zhuce.zhuce4') + '：'" prop="password" class="f-item">
                  <el-input
                    v-model="form.password"
                    style="width: 330px"
                    :type="mima1 ? 'text' : 'password'"
                    :placeholder="$t('zhuce.zhuce5')"
                    autocomplete="new-password"
                    maxlength="100"
                  />
                  <div class="yanjin" @click="mima1 = !mima1">
                    <img v-if="!mima1" src="@/assets/img/yj-bi.png" alt />
                    <img v-else src="@/assets/img/yj-kai.png" alt />
                  </div>
                </el-form-item>
                <el-form-item :label="$t('zhuce.zhuce6') + '：'" prop="password2" class="f-item">
                  <el-input
                    v-model="form.password2"
                    style="width: 330px"
                    :type="mima2 ? 'text' : 'password'"
                    :placeholder="$t('zhuce.zhuce7')"
                    maxlength="100"
                  />
                  <div class="yanjin" @click="mima2 = !mima2">
                    <img v-if="!mima2" src="@/assets/img/yj-bi.png" alt />
                    <img v-else src="@/assets/img/yj-kai.png" alt />
                  </div>
                </el-form-item>
                <el-form-item :label="$t('zhuce.zhuce8') + '：'" prop="code" class="formItem_code">
                  <el-input
                    v-model="form.code"
                    style="width: 330px"
                    :placeholder="$t('zhuce.zhuce9')"
                    maxlength="100"
                  />
                  <el-tooltip
                    class="box-item"
                    effect="dark"
                    v-if="!isShow"
                    :content="buttonName"
                    placement="top"
                  >
                    <span style="width: 80px" class="ellipsis code_btn" @click="sendCodeHandle">{{
                      buttonName
                    }}</span>
                  </el-tooltip>
                  <el-tooltip
                    class="box-item"
                    effect="dark"
                    v-if="isShow"
                    :content="buttonName"
                    placement="top"
                  >
                    <span style="width: 80px" class="ellipsis code_btn">{{ buttonName }}</span>
                  </el-tooltip>
                </el-form-item>
                <el-form-item :label="$t('zhuce.zhuce10') + '：'" prop="realName">
                  <el-input
                    v-model="form.realName"
                    style="width: 330px"
                    :placeholder="$t('zhuce.zhuce11')"
                    maxlength="100"
                  />
                </el-form-item>
              </el-form>
              <!-- 用户协议和微信登录 -->
              <div class="organization_else">
                <div class="organization_agreement pointer">
                  <el-checkbox v-model="checkeds" />
                  <div style="margin-left: 7px" @click="userAgreementHadle">
                    {{ $t('zhuce.zhuce12') }}
                    <span style="color: #003e67" @click.stop="toXieyi">{{
                      $t('zhuce.zhuce13')
                    }}</span>
                  </div>
                </div>
              </div>
              <div class="organization_item_btn">
                <el-button
                  style="width: 100px; margin-right: 10px"
                  round
                  plain
                  :size="optionButtonSize"
                  @click="cancleHandle"
                  >{{ $t('zhuce.zhuce14') }}</el-button
                >
                <el-button
                  style="width: 100px; margin-right: 10px"
                  round
                  type="primary"
                  :size="optionButtonSize"
                  @click="submitHandle"
                  >{{ $t('zhuce.zhuce15') }}</el-button
                >
              </div>
            </div>

            <!-- 其他注册方式 -->
            <div class="other_way_box">
              <div class="tip">
                <span></span>
                {{ $t('zhuce.zhuce32') }}
                <span></span>
              </div>
              <div class="swtich_type_box">
                <span @click="switchType('1')" :class="[registoType === '1' ? 'currBg' : '']">
                  <i
                    class="iconfont icon-youjian"
                    :style="{ color: registoType === '1' ? '#ffffff' : '#2c2c2c' }"
                  ></i>
                </span>
                <span @click="switchType('2')" :class="[registoType === '2' ? 'currBg' : '']">
                  <i
                    class="iconfont icon-shouji"
                    :style="{ color: registoType === '2' ? '#ffffff' : '#2c2c2c' }"
                  ></i>
                </span>
              </div>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import switchLang from '@/components/lang/index'; // 全局切换语言组件
import { messages } from '@/i18n';
import langs from '@/i18n/langs';
import { isMobile, isEmail } from '@/utils/validate';
export default {
  name: 'BLZMasterSelection',

  data() {
    let validateEmail = (rule, value, callback) => {
      if (this.registoType === '1') {
        if (value.includes('@')) {
          // 是邮箱
          if (!isEmail(value)) {
            callback(this.$t('zhuce.zhuce22'));
          }
        }
        // else {
        //   if (!isMobile(value)) {
        //     callback(this.$t('zhuce.zhuce22'));
        //   }
        // }
      } else if (this.registoType === '2') {
        // 手机号
        if (!isMobile(value)) {
          callback(this.$t('zhuce.zhuce18'));
        }
      }

      callback();
    };
    let validatePwd = (rule, value, callback) => {
      let pwdRule = new RegExp(
        /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[a-zA-Z])[0-9a-zA-Z!#$%&'()*+,-./:;<=>?@~!%^¥,\\.]{8,32}$/,
      );

      if (pwdRule.test(value)) {
        callback();
      } else {
        callback(this.$t('zhuce.zhuce31'));
      }
    };
    let validateCode = (rule, value, callback) => {
      if (value) {
        callback();
      } else {
        callback(this.$t('zhuce.zhuce24'));
      }
    };
    let validateRealName = (rule, value, callback) => {
      if (value) {
        callback();
      } else {
        callback(this.$t('zhuce.zhuce26'));
      }
    };
    return {
      // 绑定手机号区号
      phoneCode: '85',
      phoneCodeList: [],
      // 定义是邮箱注册还是手机注册
      registoType: '1',
      language: 'cn',
      codeLoading: false,
      clickCount:0,
      count: 60,
      isSendCode: false,
      isShow: false,
      checkeds: true,
      form: {
        code: '', // 验证码
        password: '',
        password2: '',
        phoneOrEmail: '',
        realName: '', // 姓名
      },
      rules: {
        code: [
          {
            validator: validateCode,
            trigger: 'blur',
          },
        ],
        password: [
          {
            validator: validatePwd,
            trigger: 'blur',
          },
        ],
        password2: [
          {
            validator: validatePwd,
            trigger: 'blur',
          },
        ],
        phoneOrEmail: [
          {
            validator: validateEmail,
            trigger: 'blur',
          },
        ],
        realName: [
          {
            validator: validateRealName,
            trigger: 'blur',
          },
        ],
      },
      mima1: false,
      mima2: false,
      // 控制语言切换的三角形
      isOpen: false,
    };
  },
  computed: {
    buttonName() {
      let str = this.$t('zhuce.zhuce16');
      if (this.isSendCode) {
        if (this.isShow) {
          str = this.count + this.$t('zhuce.zhuce28');
        } else {
          str = this.$t('zhuce.zhuce29');
        }
      } else {
        str = this.$t('zhuce.zhuce21');
      }

      return str;
    },
  },
  components: {
    switchLang,
  },
  created() {
    // 获取手机号区号数据
    this.getPhoneArea();
  },
  mounted() {
    let type = window.localStorage.getItem('lang');
    if (!type) {
      type = 'cn';
    }
    localStorage.setItem('lang', type);
    this.language = type;
    this.$i18n.locale = type;
  },

  methods: {
    getPhoneArea() {
      this.$http
        .get('/sys/params/page?page=1&limit=9999&paramCode=phoneNumber&paramType=1')
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          } else {
            this.phoneCodeList = res.data.list;
          }
        })
        .catch((err) => {
          console.log('err', err);
        });
    },
    // 获取验证码
    sendCodeHandle() {
      if (this.registoType === '1') {
        // 邮箱
        if (this.form.phoneOrEmail.includes('@')) {
          // 是邮箱
          if (!isEmail(this.form.phoneOrEmail)) {
            return this.$message.error(this.$t('zhuce.zhuce17'));
          }
        }
      } else if (this.registoType === '2') {
        // 手机号
        if (!isMobile(this.form.phoneOrEmail)) {
          return this.$message.error(this.$t('zhuce.zhuce17'));
        }
      }
      this.clickCount++;
      if (this.clickCount !== 1) {
        return false;
      }
      if (this.registoType === '1') {
        this.$http
          .get(
            `/open/sms/sendSms?uniqueKey=${
              this.form.phoneOrEmail
            }&type=REGISTER_PHONE_EMAIL&lang=${localStorage.getItem('lang')}`,
          )
          .then(({ data: res }) => {
            if (res.code !== 0) {
              this.clickCount = 0;
              return this.$message.error(res.msg);
            } else {
              // this.form.code = res.data
              this.$message.success(this.$t('zhuce.zhuce30'));
              this.count = 60;
              this.isShow = true;
              this.isSendCode = true;
              const timer = setInterval(() => {
                if (this.count > 0 && this.count <= 60) {
                  this.count--;
                  // this.buttonName = this.count + this.$t('zhuce.zhuce28');
                } else {
                  this.isShow = false;
                  this.clickCount = 0;
                  // this.buttonName = this.$t('zhuce.zhuce29');
                  clearInterval(timer);
                }
              }, 1000);
            }
          })
          .catch((err) => {
            this.clickCount = 0;
            console.log('err', err);
          });
      } else if (this.registoType === '2') {
        let formData = new FormData();
        formData.append('phone', this.form.phoneOrEmail);
        this.$http
          .post('/smsMessage/send', formData)
          .then((res) => {
            if (res.data.code !== 0) {
              this.clickCount = 0;
              return this.$message.error(res.data.msg);
            } else {
              this.$message.success(this.$t('zhuce.zhuce36'));
              this.count = 60;
              this.isShow = true;
              this.isSendCode = true;
              const timer = setInterval(() => {
                if (this.count > 0 && this.count <= 60) {
                  this.count--;
                  // this.buttonName = this.count + this.$t('zhuce.zhuce28');
                } else {
                  this.isShow = false;
                  this.clickCount = 0;
                  // this.buttonName = this.$t('zhuce.zhuce29');
                  clearInterval(timer);
                }
              }, 1000);
            }
          })
          .catch((e) => {
            this.clickCount = 0;
            console.error(e);
          });
      }
    },
    toXieyi() {
      this.openWindow(
        `${window.location.protocol}//${window.location.host}/#/xieyi`,
        this.$t('personInfo.xinxi58'),
      );
    },
    openWindow(url, title) {
      const newWindow = window.open('about:blank', title);
      newWindow.document.title = title;
      let iframe = document.createElement('iframe');
      iframe.src = url;
      iframe.style.width = '100%';
      iframe.style.height = '100vh';
      iframe.style.margin = '0';
      iframe.style.padding = '0';
      iframe.style.overflow = 'hidden';
      iframe.style.border = 'none';
      newWindow.document.body.style.margin = '0';
      newWindow.document.body.appendChild(iframe);
    },
    // 取消
    cancleHandle() {
      this.$router.push('/login');
      this.form = {
        code: '', // 验证码
        password: '',
        password2: '',
        phoneOrEmail: '',
        realName: '', // 姓名
      };

      // this.buttonName = this.$t('zhuce.zhuce21');
      this.count = 60;
      this.isShow = false;
      this.checkeds = true;
    },
    switchType(type) {
      this.registoType = type;
      this.form = {
        code: '', // 验证码
        password: '',
        password2: '',
        phoneOrEmail: '',
        realName: '', // 姓名
      };
    },
    // 注册
    submitHandle() {
      if (!this.checkeds) {
        return this.$message.error(this.$t('zhuce.zhuce27'));
      }

      this.$refs['form'].validate((valid) => {
        if (valid) {
          let url = '';
          if (this.registoType === '1') {
            url = '/open/register';
          } else if (this.registoType === '2') {
            url = 'open/registerPhone';
          }
          this.$http
            .post(url, {
              ...this.form,
              lang: localStorage.getItem('lang'),
            })
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              } else {
                this.$router.push('/login');
              }
            })
            .catch((err) => {
              console.log('err', err);
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    userAgreementHadle() {
      console.log(this.checkeds, '8');
      // 用户协议点击
      this.checkeds = !this.checkeds;
    },
  },
};
</script>

<style lang="scss">
.registerSelection {
  .el-form-item--small.el-form-item {
    margin-bottom: 45px;
  }
}

.formItem_code {
  position: relative;

  .code_btn {
    position: absolute;
    right: 18px;
    top: 10px;
    color: #003e67;
    cursor: pointer;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 25px;
    color: #003e67;
    // width: 70px;
  }
}

.login {
  height: 100vh;
  padding: 0;
  margin: 0;
}

//
.login_img {
  width: 100%;
  height: 100vh;
  background-image: url(~@/assets/img/login/bg_left.png);
  background-size: cover;
}

.bg_img {
  background-image: url(~@/assets/img/login/bg_right.png);
  background-size: cover;
}

// 右侧
.login_right {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;

  .language_dropdown {
    padding: 15px 30px;

    position: absolute;
    top: 50px;
    right: 50px;
    background: rgb(235 235 235 / 50%);
    display: flex;
    align-items: center;
    justify-content: center;

    .el-dropdown-link {
      i {
        font-size: 16px;
      }
    }

    img {
      display: inline-block;
      width: 20px;
      height: 20px;
      margin-right: 5px;
    }

    .el-dropdown {
      cursor: pointer;
    }
  }

  .logo {
    position: absolute;
    top: 0%;
    left: 0;
    width: 288px;
    height: 120px;

    background-size: cover;
  }
}

.organization1 {
  width: 384px;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  color: #333333;

  .organization_item {
    .el-input--small .el-input__inner {
      height: 45px;
    }

    display: flex;
    flex-direction: column;
    align-items: center;

    .el-form-item__label {
      padding-top: 12px;
      font-size: 14px;
      font-weight: 500;
      line-height: 20px;
      color: #333333;
    }

    // span{
    //   font-size: 14px;
    //   font-weight: 500;
    //   line-height: 20px;
    //   color: #333333;
    //   margin-bottom: 26px;
    //   margin-top: 26px;
    // }
  }
}

.organization_title {
  width: 100%;
  margin-bottom: 0px;

  .organization_title_wordage {
    height: 49px;
    text-align: center;
    font-size: 35px;
    font-family: PingFang SC;
    font-weight: 600;
    line-height: 49px;
    color: #333333;
    opacity: 1;
  }

  .organization_title_way {
    margin: 48px 0;
    padding: 0 23%;
    width: 100%;
    display: flex;
    align-content: center;
    //justify-content: space-between;
    justify-content: center;
  }
}

.f-item {
  width: 100%;
  position: relative;

  .yanjin {
    width: 16px;
    cursor: pointer;
    position: absolute;
    right: 10px;
    top: 5px;
  }
}

.transform_open {
  transform: rotate(180deg);
}

.transform_close {
  transform: rotate(0deg);
}

.other_way_box {
  .tip {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 24px;
    height: 17px;
    font-size: 12px;
    color: #a5acbe;
    line-height: 17px;
    span {
      display: inline-block;
      width: 90px;
      height: 0.5px;
      background: #e0e4ea;
      margin-right: 5px;
    }
    span:last-child {
      margin-left: 5px;
    }
  }
  .swtich_type_box {
    text-align: center;
    margin-top: 20px;
    position: relative;
    left: -10px;
    span {
      display: inline-block;
      border: 2px solid #003e67;
      border-radius: 50%;

      cursor: pointer;
      padding: 5px;
      i {
        font-size: 27px;
      }
    }
    span:nth-child(1) {
      margin-right: 10px;
    }
    span:nth-child(1) {
      margin-left: 10px;
    }
    .currBg {
      background: #003e67;
    }
  }
}
</style>
